rm(list=ls())
library(data.table) 
library(dplyr)  
library(stringr)
library(DT)

library(sp) 
library(rgdal)

library(RColorBrewer)

library(plotly)

library(leaflet) 
# Read in Manaus SHP
shp_manaus <- readOGR(dsn = "GIS Maps - WGS84 Coordinates/", layer = "CENSUS_TRACTS_MANAUS")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/MacBookAir/Desktop/GoogleDrive/DATA/git/wbg-manaus/GIS Maps - WGS84 Coordinates", layer: "CENSUS_TRACTS_MANAUS"
## with 2421 features
## It has 13 fields
## Integer64 fields read as strings:  ID
## Warning in readOGR(dsn = "GIS Maps - WGS84 Coordinates/", layer =
## "CENSUS_TRACTS_MANAUS"): Z-dimension discarded
head(shp_manaus@data)
##     ID      CD_GEOCODI   TIPO   CD_GEOCODB               NM_BAIRRO
## 0 3215 130260305060048 URBANO 130260305001                  Centro
## 1 3216 130260305060049 URBANO 130260305001                  Centro
## 2 3217 130260305060050 URBANO 130260305001                  Centro
## 3 3218 130260305060051 URBANO 130260305002 Nossa Senhora Aparecida
## 4 3219 130260305060052 URBANO 130260305002 Nossa Senhora Aparecida
## 5 3220 130260305060053 URBANO 130260305003       Presidente Vargas
##    CD_GEOCODS    NM_SUBDIST CD_GEOCODD NM_DISTRIT CD_GEOCODM NM_MUNICIP
## 0 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 1 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 2 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 3 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 4 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 5 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
##   NM_MICRO           NM_MESO
## 0   MANAUS CENTRO AMAZONENSE
## 1   MANAUS CENTRO AMAZONENSE
## 2   MANAUS CENTRO AMAZONENSE
## 3   MANAUS CENTRO AMAZONENSE
## 4   MANAUS CENTRO AMAZONENSE
## 5   MANAUS CENTRO AMAZONENSE
# get the bounding box where the polygons are contained
bbox_manaus <- shp_manaus@bbox
# get the centroids of each polygons
centroids_manaus <- coordinates(shp_manaus)

shp_manaus_edit <- shp_manaus
shp_manaus_edit$long_c <- centroids_manaus[,1]
shp_manaus_edit$lat_c <- centroids_manaus[,2]
shp_manaus_edit_data <- shp_manaus_edit@data

head(shp_manaus_edit@data)
##     ID      CD_GEOCODI   TIPO   CD_GEOCODB               NM_BAIRRO
## 0 3215 130260305060048 URBANO 130260305001                  Centro
## 1 3216 130260305060049 URBANO 130260305001                  Centro
## 2 3217 130260305060050 URBANO 130260305001                  Centro
## 3 3218 130260305060051 URBANO 130260305002 Nossa Senhora Aparecida
## 4 3219 130260305060052 URBANO 130260305002 Nossa Senhora Aparecida
## 5 3220 130260305060053 URBANO 130260305003       Presidente Vargas
##    CD_GEOCODS    NM_SUBDIST CD_GEOCODD NM_DISTRIT CD_GEOCODM NM_MUNICIP
## 0 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 1 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 2 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 3 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 4 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 5 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
##   NM_MICRO           NM_MESO    long_c     lat_c
## 0   MANAUS CENTRO AMAZONENSE -60.01449 -3.133817
## 1   MANAUS CENTRO AMAZONENSE -60.01297 -3.134340
## 2   MANAUS CENTRO AMAZONENSE -60.01276 -3.137735
## 3   MANAUS CENTRO AMAZONENSE -60.03024 -3.124573
## 4   MANAUS CENTRO AMAZONENSE -60.02815 -3.123886
## 5   MANAUS CENTRO AMAZONENSE -60.03048 -3.122606
head(shp_manaus_edit_data)
##     ID      CD_GEOCODI   TIPO   CD_GEOCODB               NM_BAIRRO
## 0 3215 130260305060048 URBANO 130260305001                  Centro
## 1 3216 130260305060049 URBANO 130260305001                  Centro
## 2 3217 130260305060050 URBANO 130260305001                  Centro
## 3 3218 130260305060051 URBANO 130260305002 Nossa Senhora Aparecida
## 4 3219 130260305060052 URBANO 130260305002 Nossa Senhora Aparecida
## 5 3220 130260305060053 URBANO 130260305003       Presidente Vargas
##    CD_GEOCODS    NM_SUBDIST CD_GEOCODD NM_DISTRIT CD_GEOCODM NM_MUNICIP
## 0 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 1 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 2 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 3 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 4 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
## 5 13026030506 PRIMEIRA R.A.  130260305     MANAUS    1302603     MANAUS
##   NM_MICRO           NM_MESO    long_c     lat_c
## 0   MANAUS CENTRO AMAZONENSE -60.01449 -3.133817
## 1   MANAUS CENTRO AMAZONENSE -60.01297 -3.134340
## 2   MANAUS CENTRO AMAZONENSE -60.01276 -3.137735
## 3   MANAUS CENTRO AMAZONENSE -60.03024 -3.124573
## 4   MANAUS CENTRO AMAZONENSE -60.02815 -3.123886
## 5   MANAUS CENTRO AMAZONENSE -60.03048 -3.122606
load("FW_DATA_Manaus_IPTU_app_-_prototype/merged_tax_census_tract_level.rda")

data_census_chunk <- merged_tax_census_tract_level %>% select(1:185) 

shp_manaus_edit <- merge(shp_manaus_edit, data_census_chunk,
             by.x = "CD_GEOCODI",
             by.y = "census_tract",
             all.x = TRUE)

ncolors <- 3000
pal3 <- colorRampPalette(c("grey", "blue"))(ncolors) # A palette from grey to red

spplot(shp_manaus_edit, c("num_properties"), main = "Num. of properties", col.regions = pal3,
       sub = "", cuts = ncolors-1, col = "transparent")

labels <- sprintf(
  "<strong>Census tract ID: %s</strong><br/>Number of properties: %s <br/>delinquency_2015: %s <br/>delinquency_2016: %s<br/>delinquency_2017: %s",
  shp_manaus_edit@data$CD_GEOCODI, shp_manaus_edit@data$num_properties, shp_manaus_edit@data$delinquency_2015,
  shp_manaus_edit@data$delinquency_2016,  shp_manaus_edit@data$delinquency_2017,big.mark = " ") %>% lapply(htmltools::HTML)


# Create a continuous palette function
palette_1 <- colorNumeric(palette = "Blues", domain = shp_manaus_edit@data$num_properties)
palette_2 <- colorNumeric(palette = "Oranges", domain = shp_manaus_edit@data$delinquency_2015)
palette_3 <- colorNumeric(palette = "Oranges", domain = shp_manaus_edit@data$delinquency_2016)
palette_4 <- colorNumeric(palette = "Oranges", domain = shp_manaus_edit@data$delinquency_2017)

lft <- leaflet(shp_manaus_edit) %>%
  addTiles(group = "OSM") %>%
  addPolygons(color = "#232a35", weight = 1, smoothFactor = 1,
              opacity = 1.0, fillOpacity = 0.8,
              fillColor = ~palette_1(num_properties),
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = TRUE),
              label = labels,
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px"),
                textsize = "15px",
                direction = "auto")
           , group = "num_properties"
           ) %>%
  addPolygons(color = "#232a35", weight = 1, smoothFactor = 1,
              opacity = 1.0, fillOpacity = 0.7,
             fillColor = ~palette_2(delinquency_2015),
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = TRUE),
              label = labels,
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px"),
                textsize = "15px",
                direction = "auto")
           , group = "delinquency_2015"
              )  %>%
  addPolygons(color = "#232a35", weight = 1, smoothFactor = 1,
              opacity = 1.0, fillOpacity = 0.7,
              fillColor = ~palette_3(delinquency_2016),
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = TRUE),
              label = labels,
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px"),
                textsize = "15px",
                direction = "auto"),
              group = "delinquency_2016")  %>%
  addPolygons(color = "#232a35", weight = 1, smoothFactor = 1,
              opacity = 1.0, fillOpacity = 0.7,
             fillColor = ~palette_4(delinquency_2017),
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = TRUE),
              label = labels,
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px"),
                textsize = "15px",
                direction = "auto")
             , group = "delinquency_2017"
             )   %>%
#  Layers control
  addLayersControl(
    baseGroups = c("num_properties", "delinquency_2015", "delinquency_2016", "delinquency_2017"),
    overlayGroups = c("OSM"),
    options = layersControlOptions(collapsed = FALSE)
  )
lft